package com.zryc.shop.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.pms.entity.ProductAttribute;
import com.zryc.shop.pms.entity.ProductAttributeCategory;
import com.zryc.shop.pms.service.IProductAttributeCategoryService;
import com.zryc.shop.pms.service.IProductAttributeService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/pms/productAttribute")
@Api(tags = "商品属性参数管理接口列表")
public class ProductAttributeController {
    @Autowired
    private IProductAttributeService proAttrService;
    @Autowired
    private IProductAttributeCategoryService proAttrCateService;
    @GetMapping("/list/{cid}")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@PathVariable @ApiParam(value = "属性分类ID", required = true) Long cid,
                               @RequestParam @ApiParam(value = "类型: 0-属性 1-参数", required = true, allowableValues = "0,1") Integer type,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        QueryWrapper<ProductAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_attribute_category_id", cid)
                .eq("type", type);
        Page page = new Page(pageNum, pageSize);
        proAttrService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("创建属性(参数)")
    @Transactional
    public ResponseResult create(@RequestBody ProductAttribute productAttribute) {
        proAttrService.save(productAttribute);
        //同时更新属性分类表中的 attribute_count栏位或param_count栏位 +1
        ProductAttributeCategory category = proAttrCateService.getById(productAttribute.getProductAttributeCategoryId());
        if (productAttribute.getType()==0) {
            category.setAttributeCount(category.getAttributeCount() + 1);
        } else {
            category.setParamCount(category.getParamCount() + 1);
        }
        proAttrCateService.updateById(category);
        return new ResponseResult(200, "创建成功");
    }

    @PostMapping("/delete")
    @ApiOperation("批量删除属性(参数)")
    @Transactional//一般要写在Service方法前！！！
    public ResponseResult delete(@RequestParam @ApiParam(value = "属性ID列表", required = true, example = "1,2,3") String ids) {
        String[] idArr = ids.split(",");
        //同时更新属性分类表中的 attribute_count栏位或param_count栏位 -1
        for (String attrId:idArr) {
            ProductAttribute productAttribute = proAttrService.getById(attrId);//查到要删除的属性信息
            ProductAttributeCategory category = proAttrCateService.getById(productAttribute.getProductAttributeCategoryId());
            if (productAttribute.getType()==0) {
                category.setAttributeCount(category.getAttributeCount() - 1);
            } else {
                category.setParamCount(category.getParamCount() - 1);
            }
            proAttrCateService.updateById(category);
        }
        //删除的操作要放在更新之后来写
        proAttrService.removeBatchByIds(Arrays.asList(idArr));//字符串 -> 数组 -> 集合 -> 批量删除
        return new ResponseResult(200, "删除成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询详情")
    public ResponseResult detail(@PathVariable @ApiParam(value = "属性ID", required = true) Long id) {
        return new ResponseResult(200, "查询完成", proAttrService.getById(id));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改属性")
    public ResponseResult detail(@PathVariable @ApiParam(value = "属性ID", required = true) Long id,
                                 @RequestBody ProductAttribute productAttribute) {
        productAttribute.setId(id);
        proAttrService.updateById(productAttribute);
        return new ResponseResult(200, "修改成功");
    }

}
